package haida.pym_system.config.shiro;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * ShiroConfig
 *
 * @Description:
 * @Author: HongJie
 * @CreateTime: 2020-04-21
 */
@Configuration
public class ShiroConfig {

	@Bean
	public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
		ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
		bean.setSecurityManager(defaultWebSecurityManager);
		/*
		anon：无需认证就可以访问
		authc：必须认证了才能让问
		user：必须用有译住我功能才能用
		perms：拥有存某个资源的权限才能访问；
		role：拥有某个角色权限才能访问
		 */
		Map<String, String> filterMap = new LinkedHashMap<>();
		// System.out.println("++++++++++++++++");
		// //场地权限
		// filterMap.put("/site/addSite", "perms[manager]");
		// filterMap.put("/site/deleteSite", "perms[manager]");
		// filterMap.put("/site/updateSite", "perms[manager]");
		// filterMap.put("/site/findASite", "perms[manager]");
		// filterMap.put("/site/toaddsite", "perms[manager]");
		// filterMap.put("/site/updateSite", "perms[manager]");
		// filterMap.put("/site/toUpdateSite/*", "perms[manager]");
		// //公告管理权限
		// filterMap.put("/notice/toManageNotice", "perms[manager]");
		// filterMap.put("/notice/toAddNotice", "perms[manager]");
		// filterMap.put("/notice/updateNotice", "perms[manager]");
		// filterMap.put("/notice/deleteNotice", "perms[manager]");
		// filterMap.put("/notice/addNotice", "perms[manager]");
		// filterMap.put("/notice/toUpdateNotice", "perms[manager]");
		// filterMap.put("/notice/findANotice/{noticeID}", "perms[manager]");
		// // filterMap.put("/user/update", "perms[common]");
		// filterMap.put("/site/findAllAbleSite", "anon");
		// filterMap.put("/site/*", "authc");
		//
		// filterMap.put("/notice/findAllReserveNotice", "anon");
		// filterMap.put("/notice/findAllindexNotice", "anon");
		// filterMap.put("/notice/*", "authc");
		// //登录不需要认证
		// filterMap.put("/login", "anon");
		// filterMap.put("/tologin/*", "anon");

		// filterMap.put("/user/*", "authc");
		bean.setFilterChainDefinitionMap(filterMap);
		// // 设置登录请求
		// bean.setLoginUrl("/tologin");
		// bean.setUnauthorizedUrl("/tonoauth");


		return bean;


	}

	@Bean(name = "securityManager")
	public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
		DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
		//关联UerRealm
		securityManager.setRealm(userRealm);
		return securityManager;
	}

	// 创建realm对象，需要自定义类 1：
	@Bean(name = "userRealm")
	public UserRealm userRealm() {
		return new UserRealm();
	}


}
